<report-objects>
 <object name="customers" sql="select * from customer order by company"/>
</report-objects>

<style>
 {{{defaultstyle}}}
body {font-family: Verdana; font-size: 10px; }
ul {float: left; width: 300px; margin: 0px 40px 10px 10px; padding-left: 10px; background: white; min-height: 100px; overflow-y: auto; max-height: 500px}
.sel {border: dotted gray 1px}
li { border: solid orange 1px; draggable: true; margin: 4px 4px; padding: 5px 5px; transition: left 0.3s; transition: top 0.3s; position: relative; display: block; height: 20px; border-radius: 5px}
.fa:before {font-size: 15pt; color: orange}
li:hover {border-color: red}
h3 {margin-left: 20px}
tbody tr:hover {background: lightgreen}
table {border-collapse: collapse; background: white;}
.selected {background: yellow}
</style>

<script type="passcript">

procedure dragenter();
begin
 if Event.Target.tag='li' then
   Event.Target.css('opacity: 0.5');
end;

procedure sortlist();
begin
  $('.sel li').SortElements(
   function(e1, e2: TElement); 
   begin 
     if e1.InnerText() > e2.InnerText() then 
      Result := 1 
   else 
   if e1.InnerText() = e2.InnerText then 
    Result := 0 
  else 
   Result := -1 end, 
  true );
  document.refresh();
end;

procedure SetOrders(s: string);
begin
  $('#orders').html := s;
end;

procedure UpdateOrders();
begin
 s:='<DATAPACKET width="500">
  <METADATA autosort="true">
   <FIELDS>
    <FIELD name="ORDERNO" caption="Order No" total="count(*)" width="10%"/>
    <FIELD name="SALEDATE" caption="Sale Date" width="10%"/>
    <FIELD name="SHIPDATE" caption="Ship Date" width="10%"/>
    <FIELD name="LASTNAME" caption="Employee" width="20%"/>
    <FIELD name="AMOUNTPAID" caption="Amount Paid" total="sum"  width="15%" format="# ### ##0.00"/>
   </FIELDS>
   <SQL>
    select o.orderno, cast(saledate as date) saledate, cast(shipdate as date) shipdate,
     paymentmethod, itemstotal, amountpaid, e.lastname
    from orders o
     join employee e on e.empno=o.empno
    where custno in ({{CUSTNO}})
    order by 1
   </SQL>
  </METADATA>
 </DATAPACKET>';
 ids := $('.sel li').Attr['id'];
if ids='' then ids := '0';
// RunHtReportAsync(s, '<CONTEXT CUSTNO="'+ids+'"/>', procedure(s: string); begin  $('#orders').html := s end);
  Async(function: string; begin Result := RunHtReport(s, '<CONTEXT CUSTNO="'+ids+'"/>'); end, procedure(s: string); begin  $('#orders').html := s; $('tr').&On('click', 'this.ToggleClass(''selected'')');  end);
end;

document.addEventListener('dragenter', @dragenter);

document.addEventListener('drop', procedure(); begin 
 $('h3').HTML := Format('%d selected', [$('.sel li').Count]);
 updateorders();
end
);

document.addEventListener('dragleave',
  procedure();
   begin
    if Event.Target.tag='li' then
       Event.Target.css('opacity: 1');
   end
);

</script>

<h3>0 selected</h3>
<ul class="sortable" id="ul">
{{#customers.ROWDATA}}
<li style="background:lightyellow" id="{{CUSTNO}}" name="{{COMPANY}}"><i class="fa fa-user"></i>  {{COMPANY}}</li>
{{/customers.ROWDATA}}
</ul>


<ul class="sortable sel" >
</ul>

<button onclick="sortlist()">Sort elements</button><br>
<div>
 <div id="orders"></div>
</div>




